﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;

using DevComponents.DotNetBar;
using Collector.Helper;

namespace Collector
{
    public partial class frmLogin : Office2007Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (HttpUtility.Instance.WebBrowserPtr.IsBusy)
            {
                HttpUtility.Instance.WebBrowserPtr.Stop();
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {           
            HttpUtility.Instance.OpenUrl(GlobalVariable.m_szEmailHomePage);
            HttpUtility.Instance.WebBrowserPtr.DocumentCompleted += LoadHomePageCompleted;
        }

        private void LoadHomePageCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url == HttpUtility.Instance.WebBrowserPtr.Url)
            {//页面打开成功
                picVerify.Image = HttpUtility.Instance.GetVerifyImage("vfcode");//获取验证码图片
                HttpUtility.Instance.CallJsMethod("showVerify", true);//显示验证码

                HttpUtility.Instance.WebBrowserPtr.DocumentCompleted -= LoadHomePageCompleted;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            SetControlEnable(false);//设置控件不可用

            HtmlDocument doc = HttpUtility.Instance.Document;
            doc.GetElementById("uin").InnerText = this.txtAccount.Text;
            doc.GetElementById("pp").InnerText = this.txtPassword.Text;
            doc.GetElementById("verifycode").InnerText = this.txtVerify.Text;

            HttpUtility.Instance.ClickByID("btlogin");//点击登录，此操作后会进入邮箱            
            HttpUtility.Instance.WebBrowserPtr.DocumentCompleted += LoginCompleted;//点击登录后完成            
        }       

        private void LoginCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url == HttpUtility.Instance.WebBrowserPtr.Url)
            {
                string url = e.Url.ToString();
                if (url.StartsWith(GlobalVariable.m_szEmailHomePage))
                {
                    this.lblMessage.Text = "登录失败！";
                    SetControlEnable(true);
                }
                else
                {
                    GlobalVariable.m_szSID = ContentHelper.GetMidString(url, "sid=", "&r=");                    
                    GlobalVariable.m_szR = url.Substring(url.IndexOf("&r=") + 3);
                    GlobalVariable.m_szUIN = this.txtAccount.Text;
                    GlobalVariable.m_szPP = this.txtPassword.Text;

                    frmMain frm = (frmMain)this.Owner;
                    frm.LoginFinish();//通知主窗口登录成功

                    this.Close();
                }

                HttpUtility.Instance.WebBrowserPtr.DocumentCompleted -= LoginCompleted;
            }
        }
        
        //点击图片重新获得验证码
        private void picVerify_Click(object sender, EventArgs e)
        {
            picVerify.Image = null;
            HttpUtility.Instance.WebBrowserPtr.Stop();
            HttpUtility.Instance.OpenUrl(GlobalVariable.m_szEmailHomePage);            
            HttpUtility.Instance.WebBrowserPtr.DocumentCompleted += LoadHomePageCompleted;
        }

        //取消按钮
        private void btnCancel_Click(object sender, EventArgs e)
        {            
            this.Close();
        }

        /// <summary>
        /// 设置界面控件使能
        /// </summary>
        /// <param name="bEnable"></param>
        private void SetControlEnable(bool bEnable)
        {
            this.txtAccount.Enabled = bEnable;
            this.txtPassword.Enabled = bEnable;
            this.txtVerify.Enabled = bEnable;
            this.btnOK.Enabled = bEnable;
        }        
    }
}